package admin.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.Connect;
import model.Question;

public class QuestionDAO {
	public static List<Question> getAllQuestion(String id){
		ArrayList<Question> listQuestion = new ArrayList<Question>();
		String sql = "SELECT * FROM datatest WHERE dethi = '"+id+"'";
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			while(result.next()){
				Question ques = new Question(result.getInt("maso"), result.getString("dethi"), result.getString("question"), result.getString("a"), result.getString("b"), result.getString("c"), result.getString("d"),result.getString("answers"));
				listQuestion.add(ques);
			}
			result.close();
			state.close();
		}catch (Exception e) {
		}
		return listQuestion;
	}

	public static void removeQues(Question question) {
		String sql = "DELETE FROM `datatest` WHERE maso="+question.getIdcauhoi();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
		}
	}

	public static void updateQues(Question question) {
		String sql = "UPDATE `datatest` SET question='"+question.getCauhoi()+"',a='"+question.getA()+"',b='"+question.getB()+"',c='"+question.getC()+"',d='"+question.getD()+"',answers='"+question.getDapan()+"' WHERE maso="+question.getIdcauhoi();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}

	public static void addQuestion(Question question) {
		String sql = "INSERT INTO `datatest` (dethi,question,a,b,c,d,answers) VALUES('"+question.getMade()+"','"+question.getCauhoi()+"','"+question.getA()+"','"+question.getB()+"','"+question.getC()+"','"+question.getD()+"','"+question.getDapan()+"')";
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
